from datetime import date
from decimal import Decimal
from typing import TypedDict

import xlwt

from inventory.views.func.datatype import rust_angle_steel_list
from inventory.views.func.table_css import css1


class Temp4(TypedDict):
    name: str
    weight: Decimal
type Type4 = list[Temp4]
type Type5 = dict[str, dict[str, Decimal]]
class ScrapList(TypedDict):
    name: str
    weight: Decimal
    jjd_no: str
    car_no: str
    customer_name: str
    bill_date: str
type Type3 = list[ScrapList]


class Row:
    def __init__(self, val):
        self.num = val

    def add1(self):
        self.num += 1


# 打印指定废品销售表，直接从景智系统中提取数据
def make_fp_table(result1: Type3, result2: Type4, result3: Type5):
    # 生成表格
    xl = xlwt.Workbook(encoding='utf-8')
    sheet1 = xl.add_sheet('明细表', cell_overwrite_ok=True)
    sheet2 = xl.add_sheet('废品分类表', cell_overwrite_ok=True)
    sheet3 = xl.add_sheet('客户分类表', cell_overwrite_ok=True)

    style, style_title = css1()

    # 设置单元格长度
    for k in [sheet1, sheet2, sheet3]:
        k.col(0).width = 4500
        k.col(1).width = 4500
        k.col(2).width = 4500
        k.col(3).width = 4500
        k.col(4).width = 4500
        k.col(5).width = 4500

    # 为每个表写开头
    sheet1.write(0, 0, '日期', style_title)
    sheet1.write(0, 1, '检斤单号', style_title)
    sheet1.write(0, 2, '车号', style_title)
    sheet1.write(0, 3, '废品名称', style_title)
    sheet1.write(0, 4, '净重', style_title)
    sheet1.write(0, 5, '客户名称', style_title)

    sheet2.write(0, 0, '废品名称', style_title)
    sheet2.write(0, 1, '净重', style_title)

    sheet3.write(0, 0, '客户名称', style_title)
    sheet3.write(0, 1, '废品名称', style_title)
    sheet3.write(0, 2, '重量', style_title)
    sheet3.write(0, 3, '净重', style_title)

    # 写入第一个表
    row = Row(1)
    total1 = Decimal(0)
    for i in result1:
        sheet1.write(row.num, 0, i['bill_date'], style)
        sheet1.write(row.num, 1, i['jjd_no'], style)
        sheet1.write(row.num, 2, i['car_no'], style)
        sheet1.write(row.num, 3, i['name'], style)
        sheet1.write(row.num, 4, i['weight'], style)
        sheet1.write(row.num, 5, i['customer_name'], style)
        total1 += i['weight']
        row.add1()
    sheet1.write(row.num, 4, total1, style)

    # 写入第二个表
    row.num = 1
    for i in result2:
        sheet2.write(row.num, 0, i['name'], style)
        sheet2.write(row.num, 1, i['weight'], style)
        row.add1()
    sheet2.write(row.num, 1, total1, style)

    # 写入第三个表
    row.num = 1
    for customer_key, value in result3.items():
        start_row = row.num  # 记录当前的行数
        total_weight = Decimal(0)  # 总计
        # 填入物料名称
        for name_key, weight_value in value.items():
            sheet3.write(row.num, 1, name_key, style)
            sheet3.write(row.num, 2, weight_value, style)
            total_weight += weight_value
            row.add1()
        # 填入名称和总计
        sheet3.write_merge(start_row, row.num-1, 0, 0, customer_key, style)
        sheet3.write_merge(start_row, row.num-1, 3, 3, total_weight, style)
    sheet3.write(row.num, 3, total1, style)

    #xl.save(f'/home/xph/Desktop/公司共享文件夹/{date_range}废品.xls')
    return xl


# 打印指定锈角表格
def print_rust_angle_steel_table(data: rust_angle_steel_list, print_date: date):
    xl = xlwt.Workbook(encoding='utf-8')
    sheet1 = xl.add_sheet('Sheet1', cell_overwrite_ok=True)

    style, style_title = css1()

    # 设置单元格长度
    for k in [sheet1]:
        k.col(0).width = 3000
        k.col(1).width = 3000
        k.col(2).width = 3000
        k.col(3).width = 3000
        k.col(4).width = 3000
        k.col(5).width = 3000
        k.col(6).width = 3000
        k.col(7).width = 3000

    # 表头
    sheet1.write_merge(0, 1, 0, 7, f'天津市兆博实业有限公司锈角库存表 {print_date}', style_title)
    sheet1.write(1, 0, '规格', style_title)
    sheet1.write(1, 1, '件数', style_title)
    sheet1.write(1, 2, '规格', style_title)
    sheet1.write(1, 3, '件数', style_title)
    sheet1.write(1, 4, '规格', style_title)
    sheet1.write(1, 5, '件数', style_title)
    sheet1.write(1, 6, '规格', style_title)
    sheet1.write(1, 7, '件数', style_title)

    # 表体
    row = Row(2)
    for i in data:
        sheet1.write(row.num, 0, i['model_1'], style_title)
        sheet1.write(row.num, 1, i['piece_1'] if i['piece_1'] != 0 else '', style_title)
        sheet1.write(row.num, 2, i['model_2'], style_title)
        sheet1.write(row.num, 3, i['piece_2'] if i['piece_2'] != 0 else '', style_title)
        sheet1.write(row.num, 4, i['model_3'], style_title)
        sheet1.write(row.num, 5, i['piece_3'] if i['piece_3'] != 0 else '', style_title)
        sheet1.write(row.num, 6, i['model_4'], style_title)
        sheet1.write(row.num, 7, i['piece_4'] if i['piece_4'] != 0 else '', style_title)
        row.add1()

    return xl

